// pages/order/index.js
const app = getApp()
import Toast from "../../vant/weapp/toast/toast";
const util = require("../../utils/util");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isOrderList: true,
    tablist: [{
        value: "全部"
      },
      {
        value: "待开始"
      },
      {
        value: "进行中"
      },
      {
        value: "已退款"
      },
      {
        value: "已结束"
      },
      // { value: "退款中" },

    ],
    show: false,
    add: [],
    addIndex: 0,
    tabIndex: 0, //tab栏默认选中第一个
    orderList: [],
    listIndex: 0,
  },

  // 切换tab栏
  chooseTab(e) {
   
    this.setData({
      tabIndex: e.currentTarget.dataset.id,
    });
    this.getOrderList()
  },
  call(e) {
    console.log(e)
    let mobile = e.currentTarget.dataset.mobile
    wx.makePhoneCall({
        phoneNumber: mobile
    })
},
  onLoad(options) {
      
    if(options.status){
      this.setData({
        tabIndex: 1,
      });
      this.getOrderList()
    }
    wx.hideShareMenu()
  },
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      console.log("设置选中项 2");
      this.getTabBar().setData({
        selected: 3,
      });
    }
    this.getOrderList()
  },
  getTime() {
    const that = this
    let start = that.data.add.order.end_time * 1000
    let timestart = util.formatHS(start)
    let next_time = new Date(timestart.replace(/\-/g, "/")).setHours(
      new Date(timestart.replace(/\-/g, "/")).getHours() + that.data.add.renewal[that.data.addIndex].time
    );
    let timeEnd = util.formatH(next_time);
    that.setData({
      timestart,
      timeEnd
    })
  },
  addTime() {
    const that = this;
    console.log
    app.http.post(app.api.renewalOrder, {
      order_on: that.data.orderList[that.data.listIndex].order_on,
      happy_id: that.data.add.renewal[that.data.addIndex].happy_id
    }, (res) => {
      console.log(11, res.data.order_on)
      console.log(11, res.data)
      wx.navigateTo({
        url: '/pages/index/order/index?order_on=' + res.data.order_on + '&store_id=' + res.data.store_id + '&from=renew',
      })
      that.setData({
        show: false
      })
    })
  },
  // 续单
  renew(e) {
    const that = this
    console.log(e.currentTarget.dataset.index)
    let listIndex = e.currentTarget.dataset.index
    app.http.post(app.api.orderRenewal, {
      order_on: that.data.orderList[listIndex].order_on
    }, (res) => {
      this.setData({
        add: res.data
      })
      this.getTime()
    })
    that.setData({
      show: true,
      listIndex
    })
  },
  onClose(e) {
    this.setData({
      show: false,
    });
  },
  none() {
    return
  },
  // 取消订单
  upData(e) {
    const that=this
    wx.showModal({
      title: '提示',
        content: '确定要取消订单吗?',
      success (res) {
        if (res.confirm) {
          //这里是点击确认执行事件
      
          app.http.post(e.currentTarget.dataset.fz?app.api.makeUpdatefz:app.api.makeUpdate, {
            order_on: e.currentTarget.dataset.order
          }, res=> {   
            that.getOrderList(3)
          })
        } else if (res.cancel) {
              //这里是点击取消执行事件
        }
       }
    })


  },

    // 提前开始
    aupData(e) {
      const that=this
      wx.showModal({
        title: '提示',
          content: '确定要提前订单吗?提前不可撤回',
        success (res) {
          if (res.confirm) {
            //这里是点击确认执行事件
        
            app.http.post(app.api.amakeUpdate, {
              order_on: e.currentTarget.dataset.order
            }, res=> {   
              if (res.code === 200) {
                Toast.success("提前成功")
                that.getOrderList(2)
              } else {
                Toast.fail(res.message)
              }
             
            })
          } else if (res.cancel) {
                //这里是点击取消执行事件
          }
         }
      })
  
  
    },
  // 门禁
  opendoor(e) {
    console.log(e.currentTarget.dataset.index)
    const that = this
    let data = e.currentTarget.dataset.data
    let i = e.currentTarget.dataset.index
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        app.http.post(app.api.DoorIndex, {
          order_on: data.order_on,
          door_id: data.door_list[i].door_id,
          lat: res.latitude,
          lng: res.longitude
        }, res => {
          if (res.code === 200) {
            Toast.success("开门成功")
          } else {
            Toast.fail(res.message)
          }
        })
      },
      fail(e) {
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });

  },
  // 导航
  duo_map(e) {
    const that = this
    let data = e.currentTarget.dataset.data;

    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        wx.openLocation({
          //​使用微信内置地图查看位置。
          latitude: Number(data.lat), //要去的纬度-地址
          longitude: Number(data.lng), //要去的经度-地址
          name: data.store_name,
          address: data.address,
          scale: 18,
        });
      },
      fail(e) {
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });
  },
  // 打开设置
  openSet() {
    var that = this;
    if (wx.getStorageSync("openSet")) {
      wx.getSetting({
        success: (res) => {
          var statu = res.authSetting;
          if (!statu["scope.userLocation"]) {
            //没授权
            wx.showModal({
              title: '授权',
              content: '如果需要使用定位功能，请点击确定并在授权管理中打开“设置”，打开定位功能。',
              complete: (res) => {
                if (res.confirm) {
                  wx.openSetting({
                    success: function (res) {
                      if (res.authSetting["scope.userLocation"]) {
                        wx.setStorageSync("openSet", false);
                      } // 如果成功打开授权
                      else {
                        wx.setStorageSync("openSet", true);
                        that.setData({
                          latitude: 0,
                          longitude: 0,
                        });
                      } // 如果用户依然拒绝授权
                    },
                    fail: function () {
                      //调用失败，授权登录不成功
                      return;
                    },
                  });
                }

                if (res.cancel) {
                  that.setData({
                    latitude: 0,
                    longitude: 0,
                  });
                }
              }
            })

          }
        },
      });
    }
  },
  getOrderList(a) {
    const that = this
    app.http.post(app.api.UserOrder, {
      status: that.data.tabIndex
    }, (res) => {
      if (res.data == "" || res.code === 401) {
        this.setData({
          orderList: res.data,
        
          isOrderList: true
        })
      } else {
        this.setData({
       
          orderList: res.data,
          isOrderList: false
        })
      }
    })
  },
  // 删除订单
  delete(e) {
    const that = this
    console.log(e.currentTarget.dataset.order)
    app.http.post(app.api.Delete, {
      order_on: e.currentTarget.dataset.order
    }, (res) => {
      console.log(res)
      that.getOrderList(3)
    })
  },
  bindAddChange(e){
    console.log(e)
this.setData({
  addIndex:e.detail.value
})
  },
  onShareAppMessage: function (res) {
    console.log(303, res)
    // 分享
    if (res.from === "button") {
      // 来自页面内转发按钮
      //console.log("转发：" + JSON.stringify(res.target))
      console.log(303, res.target.dataset.data.head_url)
    }
    return {
      title: res.target.dataset.data.name,
      path: "/pages/index/invited/index?order_on=" + res.target.dataset.data.order_on,
      imageUrl: res.target.dataset.data.head_url,
      success: function (res) {
        //console.log('/pages/order/detail/detail?goodsId=' + this.data.goodsInfo.id )
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        //console.log("转发失败:" + JSON.stringify(res));
      },
    };
  },
});